// component/changeCity/changeCity.js
import util from '../../utils/util.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    city: {
      type: String,
      value: '',
      observer: function (city) {
        this.data.proviceList.forEach((provinceData) => {
          provinceData.citylist.forEach((cityData) => {
            if (cityData.value == city) {
              this.setData({
                cityList: provinceData.citylist,
                province: provinceData.value
              })
            }
          })
        })
      }
    }
  },


  /**
   * 组件的初始数据
   */
  data: {
    province: "广东",
    // city: "",
    proviceList: [],
    cityList: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindProv(e) {
      this.setData({
        province: this.data.proviceList[e.detail.value].value,
        cityList: this.data.proviceList[e.detail.value].citylist,
        city: '选择城市'
      })
      this.properties.city = "选择城市"
    },
    bindCity(e) {
      this.properties.city = this.data.cityList[e.detail.value].value
      this.triggerEvent('changeCity', {
        cityCode: this.data.cityList[e.detail.value].code,
        city: this.data.cityList[e.detail.value].value
      })
    }
  },
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      util.request('zhuanti.php', {
        action: 'get_city'
      }).then(e => {
        this.setData({
          proviceList: e.data.data,
          cityList: e.data.data[19].citylist
        })
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
})